#include "stm32f10x.h"                  // Device header
#include "LED.h"

uint8_t Serial_Tx_Data[4];
uint8_t Serial_Rx_Data[4];
uint8_t Serial_Rx_flag;
void Serial_Init(void)
{
/*
**初始化USART3外设
*/
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
	                                                   
	//IO初始化
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init( GPIOC, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init( GPIOC, &GPIO_InitStructure);
	
	//引脚重映射
	GPIO_PinRemapConfig( GPIO_PartialRemap_USART3, ENABLE);
	
	//USART初始化配置
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init( USART3, &USART_InitStructure);
	
	//中断初始化
	USART_ITConfig( USART3, USART_IT_RXNE, ENABLE);	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init( &NVIC_InitStructure);	
	USART_Cmd( USART3, ENABLE);
}

/*
**发送一个字节的数据
*/
void Serial_SendData(uint8_t Byte)
{
	USART_SendData(USART3, Byte);
	while( USART_GetFlagStatus( USART3, USART_FLAG_TXE) == RESET);	
}



/*
**发送长度为2的数组
*/
void Serial_SendArray(uint8_t * Array)
{
	USART_SendData(USART3, Array[0]);
	while( USART_GetFlagStatus( USART3, USART_FLAG_TXE) == RESET);
	USART_SendData(USART3, Array[1]);
	while( USART_GetFlagStatus( USART3, USART_FLAG_TXE) == RESET);
	USART_SendData(USART3, Array[2]);
	while( USART_GetFlagStatus( USART3, USART_FLAG_TXE) == RESET);
	USART_SendData(USART3, Array[3]);
	while( USART_GetFlagStatus( USART3, USART_FLAG_TXE) == RESET);
}

/*
**发送包含两个数据的数据包
*/
void Serial_SendPacket(void)
{
	Serial_SendData(0xFF);
	Serial_SendArray(Serial_Tx_Data);
	Serial_SendData(0xFE);
}


/*
**获取Rx标志位状态并使Rx标志位清零
*/
uint8_t Serial_GetRxFlag(void)
{
	if ( Serial_Rx_flag == 1)
	{
		Serial_Rx_flag = 0;
		return 1;
	}
	return 0;
}

/*
**USART3中断函数
*/
void USART3_IRQHandler(void)
{
//	static uint8_t RxState = 0;
//	static uint8_t pRxPacket = 0;
	if (USART_GetITStatus(USART3, USART_IT_RXNE) == SET)   
	{
		uint8_t RxData = USART_ReceiveData(USART3);
		Serial_Rx_Data[0] = RxData;
//		if (RxState == 0)
//		{
//			if (RxData == 0xFF)
//			{
//				RxState = 1;
//				pRxPacket = 0;
//			}
//		}
//		else if (RxState == 1)
//		{
//			Serial_Rx_Data[pRxPacket] = RxData;
//			pRxPacket ++;
//			if (pRxPacket >= 2)
//			{
//				RxState = 2;
//			}
//		}
//		else if (RxState == 2)
//		{
//			if (RxData == 0xFE)
//			{
//				RxState = 0;
//				Serial_Rx_flag= 1;
//			}
//		}
		Serial_Rx_Data[1] = 1;
		USART_ClearITPendingBit(USART3, USART_IT_RXNE);
	}
}
